package com.linftech.demo.service;

import com.linftech.demo.document.OrderBom;
import com.linftech.demo.document.OrderBomDetail;
import com.linftech.demo.document.OrderBomProcess;
import com.linftech.demo.repository.OrderBomRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

/**
 * Created by cofey on 2017/3/5.
 */
@Service
public class OrderBomService {

    @Autowired
    private OrderBomRepository orderBomRepository;

    public void test() {
        OrderBom orderBom = new OrderBom();
        orderBom.setId(UUID.randomUUID().toString());
        orderBom.setEntCode("E001");
        orderBom.setDocNo("1000");
        orderBom.setRecordStatus(1);
        orderBom.setRecordVersion(1l);
        orderBom.setInventoryId("1");
        orderBom.setInventoryCode("1");
        orderBom.setInventoryName("成品");
        orderBom.setAttributeCode("1");
        orderBom.setAttributeName("自制件");
        orderBom.setSupplierCode("s1");
        orderBom.setSupplierName("供应商1");
        OrderBomProcess process = new OrderBomProcess();
        process.setDepartmentCode("d1");
        process.setDepartmentName("部门1");
        process.setEmployeeCode("e1");
        process.setEmployeeName("部门负责人");
        process.setProcessCode("p1");
        process.setProcessName("工序1");
        process.setOrderQuantity(new BigDecimal("1"));
        process.setProcessPrice(new BigDecimal("10"));
        process.setProcessAmount(process.getOrderQuantity().multiply(process.getProcessPrice()));
        List<OrderBomDetail> details = new ArrayList<OrderBomDetail>();
        OrderBomDetail detail = new OrderBomDetail();
        detail.setInventoryId("2");
        detail.setInventoryCode("2");
        detail.setInventoryName("半成品");
        detail.setAttributeCode("1");
        detail.setAttributeName("自制件");
        detail.setQuantity(new BigDecimal("1"));
        detail.setOrderQuantity(process.getOrderQuantity().multiply(detail.getQuantity()));
        detail.setUnitCode("u1");
        detail.setUnitName("件");
        detail.setLossRate(10f);
        detail.setProductionQuantity(new BigDecimal(detail.getLossRate()).divide(new BigDecimal("100")).multiply(detail.getOrderQuantity()).add(detail.getOrderQuantity()));
        detail.setSupplierCode("s1");
        detail.setSupplierName("供应商1");
        detail.setSupplierPrice(new BigDecimal("10"));
        detail.setSupplierAmount(detail.getProductionQuantity().multiply(detail.getSupplierPrice()));
        detail.setChildren(getChilren(detail.getProductionQuantity()));
        details.add(detail);
        process.setDetails(details);
        orderBom.setProcess(process);
        orderBomRepository.insert(orderBom);
    }

    private OrderBomProcess getChilren(BigDecimal productionQuantity) {
        OrderBomProcess process = new OrderBomProcess();
        process.setDepartmentCode("d2");
        process.setDepartmentName("部门2");
        process.setEmployeeCode("e2");
        process.setEmployeeName("部门负责人");
        process.setProcessCode("p2");
        process.setProcessName("工序2");
        process.setOrderQuantity(productionQuantity);
        process.setProcessPrice(new BigDecimal("10"));
        process.setProcessAmount(process.getOrderQuantity().multiply(process.getProcessPrice()));
        List<OrderBomDetail> details = new ArrayList<OrderBomDetail>();
        OrderBomDetail detail = new OrderBomDetail();
        detail.setInventoryId("2");
        detail.setInventoryCode("2");
        detail.setInventoryName("物料");
        detail.setAttributeCode("1");
        detail.setAttributeName("采购件");
        detail.setQuantity(new BigDecimal("1"));
        detail.setOrderQuantity(process.getOrderQuantity().multiply(detail.getQuantity()));
        detail.setUnitCode("u1");
        detail.setUnitName("件");
        detail.setLossRate(10f);
        detail.setProductionQuantity(new BigDecimal(detail.getLossRate()).divide(new BigDecimal("100")).multiply(detail.getOrderQuantity()).add(detail.getOrderQuantity()));
        detail.setSupplierCode("s1");
        detail.setSupplierName("供应商1");
        detail.setSupplierPrice(new BigDecimal("10"));
        detail.setSupplierAmount(detail.getProductionQuantity().multiply(detail.getSupplierPrice()));
        details.add(detail);
        process.setDetails(details);
        return process;
    }
}
